/*
 *   Copyright 2012, Thomas Kerber
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package milk.jpatch.attribs;

import java.util.Arrays;
import java.util.List;

import milk.jpatch.CPoolMap;

import org.apache.bcel.classfile.Attribute;

/**
 * Patch that replaces the attributes of a certain type with a supplied
 * attribute.
 * @author Thomas Kerber
 * @version 1.0.0
 */
public class ReplaceAttributeTypePatch extends AttributePatch{
    
    private static final long serialVersionUID = -4380014566187962105L;
    
    /**
     * The type to replace.
     */
    private String replaceType;
    /**
     * The attribute to use instead.
     */
    private Attribute replaceWith;
    
    /**
     * The attribute types to generate.
     * 
     * No, this isn't final, yes, you may add your own.
     */
    public static String[] REPLACE_TYPES = new String[]{
        "AnnotationsDefault",
        "ConstantValue",
        "Deprecated",
        "EnclosingMethod",
        "Signature",
        "SourceFile",
        "Synthetic"
    };
    
    /**
     * Creates.
     * @param type The type to replace.
     * @param with The replacement.
     */
    public ReplaceAttributeTypePatch(String type, Attribute with){
        replaceType = type;
        replaceWith = with;
    }
    
    /**
     * Generates.
     * @param old The old attributes.
     * @param new_ The new attributes.
     * @param patches The already generated patches.
     * @param map The CPoolMap from new to old.
     */
    public static void generate(Attribute[] old, Attribute[] new_,
            List<AttributePatch> patches, CPoolMap map){
        for(String type : REPLACE_TYPES){
            int oldIndex = findName(Arrays.asList(old), type);
            int newIndex = findName(Arrays.asList(new_), type);
            
            if(newIndex == -1 || oldIndex == -1)
                continue;
            
            String newToString = map.applyTo(new_[newIndex]).toString();
            String oldToString = oldIndex == -1 ? null : old[oldIndex].
                    toString();
            
            if(newToString.equals(oldToString))
                continue;
            
            patches.add(new ReplaceAttributeTypePatch(type, new_[newIndex]));
        }
    }
    
    /**
     * 
     * @return The name of the type to be replaced.
     */
    public String getName(){
        return replaceType;
    }
    
    /**
     * 
     * @return The attribute to use instead.
     */
    public Attribute getReplacement(){
        return replaceWith;
    }
    
    @Override
    public List<Attribute> patch(List<Attribute> attribs, CPoolMap map){
        return replaceTypeWith(attribs, replaceType, replaceWith);
    }
    
}
